package com.example.jimingxuan

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.jimingxuan.R
import java.io.File

class HistoryActivity : AppCompatActivity() {
    private lateinit var primeListView: ListView
    private lateinit var toolbar: Toolbar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_history)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        primeListView = findViewById(R.id.primeListView)
        loadPrimes()
    }

    private fun loadPrimes() {
        val file = File(filesDir, "primes.txt")
        if (file.exists()) {
            val savedPrimes = file.readLines().map { it.toInt() }
            val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, savedPrimes)
            primeListView.adapter = adapter
        }
    }
}